<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gio-metadata-dialog__title">
  <span mat-dialog-title>
    {{ metadata.defaultValue ? 'Override global metadata' : metadata.action + ' ' + metadata.referenceType + ' metadata' }}
  </span>
  <mat-icon class="gio-metadata-dialog__title__cancel"></mat-icon>
  <button mat-icon-button [attr.aria-label]="'Close'" (click)="onClose()">
    <mat-icon class="gio-metadata-dialog__title__cancel" svgIcon="gio:cancel"> </mat-icon>
  </button>
</div>

<form [formGroup]="form" (ngSubmit)="save()">
  <mat-dialog-content>
    <div class="gio-metadata-dialog__form">
      <mat-form-field appearance="outline" *ngIf="metadata.action === 'Update'">
        <input id="key" type="text" matInput formControlName="key" data-testid="metadata_dialog_key" />
        <mat-label>Key</mat-label>
      </mat-form-field>

      <mat-form-field appearance="outline">
        <input id="name" type="text" matInput formControlName="name" required="true" data-testid="metadata_dialog_name" />
        <mat-label>Name</mat-label>
      </mat-form-field>

      <mat-form-field appearance="outline">
        <mat-label>Format</mat-label>
        <mat-select formControlName="format" data-testid="metadata_dialog_format">
          <mat-option *ngFor="let format of formats" [value]="format">{{ format.toLowerCase() }}</mat-option>
        </mat-select>
      </mat-form-field>
      <mat-form-field appearance="outline" *ngIf="metadata.action === 'Update' && metadata.defaultValue">
        <input id="defaultValue" type="text" matInput formControlName="defaultValue" />
        <mat-label>Global value</mat-label>
      </mat-form-field>

      <ng-container [ngSwitch]="form.getRawValue()?.format">
        <ng-container *ngSwitchCase="'BOOLEAN'">
          <mat-form-field appearance="outline">
            <mat-label>Value</mat-label>
            <mat-select id="value-boolean" formControlName="value" data-testid="metadata_dialog_boolean">
              <mat-option [value]="true">true</mat-option>
              <mat-option [value]="false">false</mat-option>
            </mat-select>
          </mat-form-field>
        </ng-container>

        <ng-container *ngSwitchCase="'DATE'">
          <mat-form-field appearance="outline" class="datepicker">
            <mat-label>Choose a date</mat-label>
            <div class="gio-metadata-dialog__datepicker">
              <input id="value-date" matInput [matDatepicker]="picker" formControlName="value" data-testid="metadata_dialog_date_value" />
              <mat-datepicker-toggle matIconSuffix [for]="picker">
                <mat-icon matDatepickerToggleIcon>keyboard_arrow_down</mat-icon>
              </mat-datepicker-toggle>
              <mat-datepicker #picker></mat-datepicker>
            </div>
            <mat-hint>MM/DD/YYYY</mat-hint>
            <mat-error>Please enter a valid date</mat-error>
          </mat-form-field>
        </ng-container>

        <ng-container *ngSwitchCase="'NUMERIC'">
          <mat-form-field appearance="outline">
            <mat-label>Value</mat-label>
            <input
              id="value-numeric"
              type="number"
              matInput
              formControlName="value"
              required="true"
              data-testid="metadata_dialog_numeric_value"
            />
            <mat-hint>Ex: 123000.92</mat-hint>
            <mat-error>Invalid number</mat-error>
          </mat-form-field>
        </ng-container>

        <ng-container *ngSwitchCase="'MAIL'">
          <mat-form-field appearance="outline">
            <mat-label>Value</mat-label>
            <input
              [pattern]="mailPattern"
              id="value-mail"
              type="email"
              matInput
              formControlName="value"
              required="true"
              data-testid="metadata_dialog_mail_value"
            />
            <mat-hint>Ex: john&#64;doe.com</mat-hint>
            <mat-error>Invalid email</mat-error>
          </mat-form-field>
        </ng-container>

        <ng-container *ngSwitchCase="'URL'">
          <mat-form-field appearance="outline">
            <mat-label>Value</mat-label>
            <input
              [pattern]="urlPattern"
              id="value-url"
              type="url"
              matInput
              formControlName="value"
              required="true"
              data-testid="metadata_dialog_url_value"
            />
            <mat-hint>Ex: https://gravitee.io</mat-hint>
            <mat-error>Invalid URL</mat-error>
          </mat-form-field>
        </ng-container>

        <ng-container *ngSwitchDefault>
          <mat-form-field appearance="outline">
            <mat-label>Value</mat-label>
            <input
              id="value-string"
              type="text"
              matInput
              formControlName="value"
              required="true"
              data-testid="metadata_dialog_string_value"
            />
          </mat-form-field>
        </ng-container>
      </ng-container>
    </div>
  </mat-dialog-content>
  <mat-dialog-actions align="end">
    <button mat-stroked-button type="button" [attr.aria-label]="'Cancel'" (click)="onClose()" data-testid="metadata_dialog_cancel">
      Cancel
    </button>
    <button
      [disabled]="this.form.invalid || this.form.pristine || !hasChange"
      mat-raised-button
      type="submit"
      color="primary"
      data-testid="metadata_dialog_save"
    >
      Save
    </button>
  </mat-dialog-actions>
</form>
